﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace DownloadBook {
    public class HtmlUtil {
        public static string REG_HTML_A_HREF = "<a[^>]*?href[^=]*?=[\\s]*?[\\\"|']?([^\\\"|^'|^>]+)[\\\"|']?";
        public static string REG_HTML_A_TEXT = "<a[^>]*?>([\\w\\W]*?)</a>";
        private static readonly Regex P_A_HREF = new Regex(REG_HTML_A_HREF, RegexOptions.IgnoreCase);
	    private static readonly Regex P_A_TEXT = new Regex(REG_HTML_A_TEXT, RegexOptions.IgnoreCase);


        public static String GetAHref(String htmlContext) {
            Match m = P_A_HREF.Match(htmlContext);
            String result = GroupOne(m, 1);
            return result;
        }

        public static String GetAText(String htmlContext) {
            Match m = P_A_TEXT.Match(htmlContext);
            String result = GroupOne(m, 1);
            return result;
        }


        public static String GroupOne(Match matcher, int index) {
            string result = "";
            lock(matcher) {
                try {
                    if (matcher.Success) {
                        if (matcher.Groups.Count >= index) {
                            result = matcher.Groups[index].ToString();
                        }
                    }
                } catch (Exception) {

                }
            }
            return result;
        }

        /**
         * 链接计算url - 没使用正则表达式，无需线程同步
         */
        public static String linkCalculation(String locationUrl, String hrefUrl) {
            String url = null;
            hrefUrl = hrefUrl.Replace("\\", "/");
            if (hrefUrl.IndexOf("http://") == 0 || hrefUrl.IndexOf("https://") == 0) {// 如果是完整的
                url = hrefUrl;
            } else if (hrefUrl.IndexOf("/") == 0) {// 如果是以根目录开头
                string hostName = GetHostName(locationUrl);
                url = "http://" + hostName + hrefUrl;
            } else {// 如果是相对路径
                String dir = locationUrl;
                int lastF = dir.LastIndexOf("/");
                if (lastF != dir.Length) {
                    dir = dir.Substring(0, lastF + 1);
                }
                url = dir + hrefUrl;// 其他的就会是以当前目录继续计算
            }
            return url;
        }

        /// <summary>
        /// 获取主机名称
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetHostName(string url) {
            string hostName = "";
            if (!string.IsNullOrEmpty(url)) {
                if (url.IndexOf("http://", StringComparison.OrdinalIgnoreCase) == 0) {
                    String t1 = url.Substring(7);
                    int index = t1.IndexOf("/");
                    if (index == -1) {
                        hostName = t1;
                    } else {
                        hostName = t1.Substring(0, index);
                    }
                }
                if (url.IndexOf("https://", StringComparison.OrdinalIgnoreCase) == 0) {
                    String t1 = url.Substring(8);
                    int index = t1.IndexOf("/");
                    if (index == -1) {
                        hostName = t1;
                    } else {
                        hostName = t1.Substring(0, index);
                    }
                }
            }
            return hostName;
        }
    }
}
